package stock;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;

import org.hibernate.Session;

import net.sf.json.JSONObject;

public class ListProcessorCallable implements Callable<List<JSONObject>> {

	private List<String> sublist;

    public ListProcessorCallable(List<String> sublist) {
        this.sublist = sublist;
    }
    @Override
    public List<JSONObject> call() throws Exception {
    	List<JSONObject> list = new ArrayList<JSONObject>();
        Session session1 = ContextUtil.getSessionFactory1().openSession();
    	String sql1 = " from Stock t where  t.code = ?0 ORDER BY t.date desc ";
		String sql2 = " select count(*) from Stock t where  t.code = ?0 and t.zhandifu >=5   ORDER BY t.date desc ";
		String sql3 = " select count(*) from Stock t where  t.code = ?0 and t.zhandifu <= -5  ORDER BY t.date desc ";
		String sql4 =  "select count(*),max(t.date),min(t.date) from stock t where t.`code` = ?0";
		String sqlName = "from Stock t where t.code = ?0 ";
		for (Iterator iterator = sublist.iterator(); iterator.hasNext();) {
			
			StockSelectImpl.flagA ++;
			String code = (String) iterator.next();
			System.out.println("--------正在计算--" + code + "-------------------"+StockSelectImpl.flagA ); 
			List<Stock> list2 = session1.createQuery(sql1).setParameter(0, code).setFirstResult(0).setMaxResults(StockSelectImpl.zhang)
					.list(); 
			Float f = 5F; 
			Object zhang5 = (Object) session1.createSQLQuery(sql2).setParameter(0, code).uniqueResult();
			Object die5 = (Object) session1.createSQLQuery(sql3).setParameter(0, code).uniqueResult();
			Stock stock = (Stock) session1.createQuery(sqlName).setParameter(0, code).setFirstResult(0).setMaxResults(1).list().get(0);
			List<Object[]> listk  = session1.createSQLQuery(sql3).setParameter(0, code).list();
			boolean b = false;
			float sum1 = 0f;
			for (int j = 0; j < list2.size(); j++) {
				Stock sc = list2.get(j);
				sum1 += sc.getZhandifu();
				if (sc.getZhandifu() < 0) {
					b = true;
					break;
				}
				if (sc.getChengjiaoliang() <= 0) {
					b = true;
					break;
				}
				if (sc.getName().indexOf("ST") >= 0 || sc.getName().indexOf("退") >= 0) {
					b = true;
					break;
				}
			}
			if (b == false) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("code", code);
				jsonObject.put("sum", sum1);
				jsonObject.put("zhang5", zhang5);
				jsonObject.put("die5", die5);
				jsonObject.put("stock", stock.getName());
				jsonObject.put("diqv", stock.getDiqv());
				Object[] obj  = listk.get(0);
				jsonObject.put("maxDate", obj[0]);
				jsonObject.put("minDate", obj[2]);
				jsonObject.put("count", obj[1]);
				jsonObject.put("cDate", DateTools.getCurrentDate(""));
				list.add(jsonObject);
			}
			System.out.println("--------符合要求--" + list.size() + "-------------------");
		}
        return list;
    }

}
